import java.io.*;
import java.net.*;

 class TCPServer {
    public static void main(String[] args) {
        try (ServerSocket serverSocket = new ServerSocket(8888)) {
            System.out.println("服务器已启动，等待连接...");

            // 监听客户端连接
            try (Socket clientSocket = serverSocket.accept();
                 PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
                 BufferedReader in = new BufferedReader(
                         new InputStreamReader(clientSocket.getInputStream()))) {

                System.out.println("客户端已连接: " + clientSocket.getInetAddress());

                // 读取客户端消息
                String inputLine;
                while ((inputLine = in.readLine()) != null) {
                    System.out.println("客户端消息: " + inputLine);

                    // 发送响应
                    out.println("服务器已收到: " + inputLine);

                    // 单条消息处理后退出循环
                    break;
                }
            }
        } catch (IOException e) {
            System.err.println("服务器异常: " + e.getMessage());
            e.printStackTrace();
        }
    }
}
